
import hashlib
from models.account import User


def hashed(text):
    """
    hash加密
    :param text:
    :return:
    """
    return hashlib.md5(text.encode()).hexdigest()


def register(username, password):
    """
    注册逻辑
    :param username:
    :param password:
    :return:
    """
    if not User.is_exists(username):
        User.add_user(username, hashed(password))
        return {"msg": "ok"}
    return {"msg": "error"}


def authenticate(username, password):
    """
    登录验证逻辑
    :param username:
    :param password:
    :return:
    """
    database_password = User.get_password(username)
    if hashed(password) == database_password:
        return True
    return False
